<template>
  <div class="list-table">
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('产品名称')" prop="productName">
        <el-input
          v-model="queryParams.productName"
          :placeholder="$t('产品名称')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('投资时间')" prop="investDateArray">
        <DatePicker
          v-model="investDateArray"
          type="daterange"
        />
      </el-form-item>
      <el-form-item :label="$t('投资轮次')" prop="investRound">
        <el-select v-model="queryParams.investRound" :placeholder="$t('投资轮次')" clearable size="small">
          <el-option
            v-for="dict in roundOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">
          {{ $t('搜索') }}
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['gp:investEvent:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="gpInvestEventList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('产品名称')" align="center" prop="product">
        <template slot-scope="scope">
          <div style="display: flex;align-items: center;">
            <img v-if="scope.row.productLogo" :src="scope.row.productLogo" width="35" height="35" style="border-radius: 50%; " @error="scope.row.productLogo = ''">
            <el-link type="primary" class="link_a" style="padding-left:5px;" @click="updateHandle(scope.row)">{{ scope.row.productName }}
            </el-link>
          </div>
        </template>
      </el-table-column>
      <el-table-column :label="$t('投资金额')" align="center" prop="investMoney" show-overflow-tooltip width="150" />
      <el-table-column :label="$t('投资时间')" align="center" prop="investDate" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.investDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('投资轮次')" align="center" prop="investRound" :formatter="roundFormat" show-overflow-tooltip width="120" />
      <el-table-column :label="$t('新闻标题')" align="center" prop="newsTitle" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link
            class="link_a"
            type="primary"
            @click="openNews(scope.row)"
          >{{ scope.row.newsTitle }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['gp:investEvent:save']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}
          </el-button>
          <el-button
            v-hasPermi="['gp:investEvent:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改投资动态对话框 -->
    <el-dialog :title="$t('投资动态')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <gpInvestEventForm ref="gpInvestEventForm" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listGpInvestEvent,
  delGpInvestEvent,
  exportGpInvestEvent
} from '@/api/fund/gp/tycInvest/gpInvestEvent'
import gpInvestEventForm from './gpInvestEventForm.vue'
import moment from 'moment'

export default {
  components: {
    gpInvestEventForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 投资动态表格数据
      gpInvestEventList: [],
      // 参与角色 字典rounds_investment字典
      roundOptions: [],
      // 是否显示弹出层
      formOpen: false,
      // 查询参数
      queryParams: {
        productName: undefined,
        gpId: this.$route.query.gpId,
        investDateStart: undefined,
        investDateEnd: undefined,
        investRound: '',
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      // 查询参数 时间数组
      investDateArray: []
    }
  },
  created() {
    this.getList()
    this.getDicts('rounds_investment').then(response => {
      this.roundOptions = response.data
    })
  },
  methods: {
    /** 查询投资动态列表 */
    getList() {
      this.loading = true
      if (this.investDateArray !== undefined) {
        if (this.investDateArray[0] !== null && this.investDateArray[0] !== undefined) {
          this.queryParams.investDateStart = moment(this.investDateArray[0]).format('YYYY-MM-DD')
        }
        if (this.investDateArray[1] !== null && this.investDateArray[1] !== undefined) {
          this.queryParams.investDateEnd = moment(this.investDateArray[1]).format('YYYY-MM-DD')
        }
      }
      listGpInvestEvent(this.queryParams).then(response => {
        this.gpInvestEventList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 轮次字典翻译
    roundFormat(row) {
      return this.selectDictLabel(this.roundOptions, row.investRound)
    },
    // 文件类型字典翻译
    fileTypeFormat(row) {
      return this.selectDictLabel(this.fileTypeOptions, row.fileType)
    },
    getDealIdMethod() {
      if (this.dealId === undefined || this.dealId === '' || this.dealId === null) {
        if (this.$route.path.includes('lpBase')) {
          this.dealId = this.$route.query.lpId
        }
      }
      this.queryParams.dealId = this.dealId
    },

    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        productName: undefined,
        productLogo: undefined,
        newsUrl: undefined,
        productId: undefined,
        investMoney: undefined,
        investRound: undefined,
        investDate: undefined,
        newsTitle: undefined,
        dealId: this.$route.query.id,
        companyId: this.$route.query.companyId,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.investDateArray = []
      this.queryParams.investDateEnd = undefined
      this.queryParams.investDateStart = undefined
      this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
      this.formOpen = true
      this.$nextTick(() => {
        this.$refs.gpInvestEventForm.init(null)
      })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.gpInvestEventForm.init(row)
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
      this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return delGpInvestEvent(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess(this.$t('删除成功'))
      }).catch(function() {
      })
    },
    /** 导出按钮操作 */
    exportHandle() {
      const queryParams = this.queryParams
      this.$confirm(this.$t('是否确认导出所有投资动态数据项?'), this.$t('警告'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(function() {
        return exportGpInvestEvent(queryParams)
      }).then(response => {
        this.download(response.msg)
      }).catch(function() {
      })
    },
    openNews(row) {
      if (row.newsUrl === '' || row.newsUrl === undefined) {
        return
      }
      if (row.newsUrl.indexOf('http://') !== -1) {
        window.open(row.newsUrl)
      } else if (row.newsUrl.indexOf('https://') !== -1) {
        window.open(row.newsUrl)
      } else {
        window.open('http://' + row.newsUrl)
      }
    }
  }
}
</script>
